package activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.laoxinwen.app.R;

import activity.main.MainActivity;
import common.BaseActivity;
import common.UserConfig;
import gallery.DotView;

/**
 * @author ilioili
 */
public class WelcomeActivity extends BaseActivity implements View.OnClickListener {
    ViewPager viewPager;
    DotView dotView;
    private View[] pages = new View[3];

    @Override
    public void onBackPressed() {
    }

    @Override
    protected void onLeftSlide() {
        if (viewPager.getCurrentItem() == 2) {
            onClick(null);
        }
    }

    @Override
    public void onClick(View v) {
        UserConfig.setFirstUse(false);
        Intent it = new Intent(WelcomeActivity.this, MainActivity.class);
        startActivity(it);
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);
        viewPager = (ViewPager) findViewById(R.id.pager);
        dotView = (DotView) findViewById(R.id.dotView);
        int radius = (int) (getResources().getDisplayMetrics().density * 2.5f);
        dotView.set(3, getResources().getColor(R.color.theme_blue_light), getResources().getColor(R.color.theme_blue), radius);
        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(new PagerAdapter() {

            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View v = pages[position];
                if (v == null) {
                    switch (position) {
                        case 0:
                            v = getLayoutInflater().inflate(R.layout.welcome_page_1, container, false);
                            break;
                        case 1:
                            v = getLayoutInflater().inflate(R.layout.welcome_page_2, container, false);
                            break;
                        case 2:
                            v = getLayoutInflater().inflate(R.layout.welcome_page_3, container, false);
                            break;
                    }
                    pages[position] = v;
                }
                container.addView(v);
                return v;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view.equals(object);
            }
        });
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageSelected(int position) {
                dotView.setCurIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                // TODO Auto-generated method stub

            }
        });
    }
}
